package com.manfred.uddi.test;

import org.uddi4j.client.UDDIProxy;
import org.uddi4j.response.AuthToken;
import org.uddi4j.transport.TransportFactory;
import org.uddi4j.datatype.business.BusinessEntity;
import org.uddi4j.response.BusinessDetail;
import org.uddi4j.datatype.business.Contact;
import org.uddi4j.datatype.business.Contacts;
import org.uddi4j.util.KeyedReference;
import org.uddi4j.util.IdentifierBag;
import org.uddi4j.util.CategoryBag;
import org.uddi4j.datatype.tmodel.TModel;

import java.util.Vector;

public class CreateNewBusiness {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static void main(String args[]) {
		// String inquiryURL = "http://localhost:8080/juddi/inquiry";
		// String publishURL = "http://localhost:8080/juddi/publish";
		String inquiryURL = "http://localhost:8080/juddiv3/services/inquiryv2";
		String publishURL = "http://localhost:8080/juddiv3/services/publishv2";

		String userId = "juddi";
		String credential = "juddi";

		String businessName = "The Daily Moon";
		String contact_personName = "Pat Moonie";
		String identifier_homepage = "http://www.daily-moon.com";
		String category_NAICS = "511110";

		String transportClass = "org.uddi4j.transport.ApacheAxisTransport";
		System.setProperty(TransportFactory.PROPERTY_NAME, transportClass);

		UDDIProxy proxy = new UDDIProxy();

		try {

			proxy.setInquiryURL(inquiryURL);
			proxy.setPublishURL(publishURL);

			AuthToken token = proxy.get_authToken(userId, credential);
			System.out.println("Security authToken:"
					+ token.getAuthInfoString());

			Vector entities = new Vector();

			BusinessEntity newBusiness = new BusinessEntity("", businessName);

			Contact newContact = new Contact(contact_personName);
			Vector contactsVector = new Vector();
			contactsVector.addElement(newContact);
			Contacts contacts = new Contacts();
			contacts.setContactVector(contactsVector);
			newBusiness.setContacts(contacts);

			KeyedReference taxid = new KeyedReference(
					TModel.HOMEPAGE_TMODEL_KEY, identifier_homepage);
			taxid.setTModelKey(TModel.NAICS_TMODEL_KEY);
//			taxid.setTModelKey("uddi:juddi.apache.org:960132cb-7f97-433a-b9af-47c3eebbab8d");
			IdentifierBag idBag = new IdentifierBag();
			idBag.add(taxid);
			newBusiness.setIdentifierBag(idBag);

			KeyedReference category = new KeyedReference(
					TModel.NAICS_TMODEL_KEY, category_NAICS);
			category.setTModelKey(TModel.NAICS_TMODEL_KEY);
			CategoryBag catBag = new CategoryBag();
			catBag.add(category);
			newBusiness.setCategoryBag(catBag);

			entities.addElement(newBusiness);

			BusinessDetail bd = proxy.save_business(token.getAuthInfoString(),
					entities);

			Vector businessEntities = bd.getBusinessEntityVector();
			BusinessEntity returnedBusinessEntity = (BusinessEntity) (businessEntities
					.elementAt(0));
			System.out.println("The new businessKey:"
					+ returnedBusinessEntity.getBusinessKey());

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
